Skip to content

Propagate guest exit codes and implement host-mediated time.sleep - #20

Merged
danbugs merged 2 commits into
mainfrom
fix/exit-code-and-sleep
May 13, 2026
Merged

Propagate guest exit codes and implement host-mediated time.sleep#20
danbugs merged 2 commits into
mainfrom
fix/exit-code-and-sleep

Conversation

@danbugs

@danbugs danbugs commented May 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Exit code propagation: Replace PyRun_SimpleString (which swallows exceptions) with PyRun_String + manual exception handling. SystemExit codes are extracted (e.g. sys.exit(42) → exit code 42), other exceptions set exit code 1 with traceback printed. Non-zero codes reported to host via __hl_exit /dev/hcall side channel.
  • Host-mediated time.sleep: Unikraft's cooperative scheduler on Hyperlight has no timer interrupt, so nanosleep returns immediately. Monkey-patch time.sleep during init to route through __hl_sleep host call, which blocks the host thread for the requested duration.
  • Host API: Sandbox gains last_exit_code() / reset_exit_code() backed by Arc<AtomicI32>. RunTiming exposes exit_code. pyhl binary exits with the guest's reported code.

Three behavioral fixes for python-agent-driver on Hyperlight:

1. Exception exit codes: PyRun_SimpleString swallowed exceptions.
   Replace with PyRun_String + manual exception handling in
   run_code_with_exceptions(). Non-zero exit codes reported to
   host via __hl_exit /dev/hcall side channel.

2. sys.exit(N) propagation: SystemExit was caught by CPython's
   internal handler which called exit() — Hyperlight's VmExit::Halt
   doesn't carry exit codes. Now intercepted before CPython handles
   it, code extracted and reported via __hl_exit.

3. time.sleep() was a no-op: Unikraft's cooperative scheduler on
   Hyperlight has no timer interrupt, so nanosleep returns
   immediately. Monkey-patch time.sleep during init to route
   through __hl_sleep host call, which blocks the host thread.

Host side: Sandbox gains exit_code field (Arc<AtomicI32>), always
registers __dispatch with __hl_exit and __hl_sleep handlers via
register_internal_tools(). RunTiming exposes exit_code. pyhl binary
exits with the guest's reported code.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs
danbugs marked this pull request as ready for review May 13, 2026 02:06
@danbugs
danbugs enabled auto-merge May 13, 2026 02:06

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux Benchmarks

Details
Benchmark suite Current: 2270ef1 Previous: 35c26ff Ratio
hello_world (median) 20 ms 20 ms 1
pandas (median) 100 ms 100 ms 1
density (per VM) 6 MB 6 MB 1
snapshot (disk) 648 MiB 648 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows Benchmarks

Details
Benchmark suite Current: 2270ef1 Previous: 35c26ff Ratio
hello_world (median) 237 ms 281 ms 0.84
pandas (median) 819 ms 983 ms 0.83
density (per VM) 6 MB 6 MB 1
snapshot (disk) 673 MiB 673 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs
danbugs merged commit 6e0b38e into main May 13, 2026
74 checks passed
@danbugs
danbugs deleted the fix/exit-code-and-sleep branch May 13, 2026 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant